package com.karina.visualization.chart.component;

import java.util.ArrayList;
import java.util.List;

import com.karina.visualization.chart.model.ChartModel.DataDescriptor;
import com.karina.visualization.chart.model.SeriesModel;
import com.karina.visualization.chart.model.SeriesModel.CategoryValue;
import com.karina.visualization.chart.model.ValueSeriesModel;

public class DefaultValueDataDescriptor implements DataDescriptor<List<String>> {

	@Override
	public String getDescription(List<String> item) {
		return getLabel(item);
	}

	@Override
	public String getLabel(List<String> item) {
		return item.get(0);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<SeriesModel> createSeries(List<List<String>> items) {
		List<SeriesModel> ret = new ArrayList<SeriesModel>();
		double total = 0;
		for (List<String> item : items) {
			ValueSeriesModel series = new ValueSeriesModel();

			series.setName(getLabel(item));

			CategoryValue value = new CategoryValue();
			value.setCategory(getLabel(item));
			value.setDescription(getDescription(item));
			series.setName(getLabel(item));
			Double v = Double.parseDouble(item.get(1));
			total += v;
			value.setValue(v);
			series.setValue(value);
			ret.add(series);
		}
		double startfrom = 0.0;
		int i = 0;
		for (SeriesModel<CategoryValue> series : ret) {
			series.setRelativeX(startfrom);
			CategoryValue categoryValue = series.getValue();
			double portion = Double.parseDouble(categoryValue.getValue()
					.toString()) / total;
			categoryValue.setValue(portion);
			startfrom += portion;
			i++;
			series.setIndex(i);
		}

		return ret;
	}
}
